package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoService implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMappe;
    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @Override
    public UserInfo get(Long id) {
        return userInfoMappe.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        int count = userInfoMappe.selectCountByPhone(phone);
        return count>0;
    }

    @Override
    public void sendVerifyCode(String phone) {
        //生成验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        //拼接字符串   设置100个
        StringBuilder sb = new StringBuilder(100);
        //进行拼接
        sb.append("你注册的验证码是:").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");
        //输出验证码
        System.err.println(sb);
        userInfoRedisService.sendVerifyCode(phone,code);

    }
    @Override
    public void userRegist(String phone,String nickname, String password, String rpassword, String verifyCode)throws LogicException
    {
        //判断参数 写工具类
        AssertUtil.hasLength(phone,"手机号不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"验证密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");
        //必须校验密码是否相等
        AssertUtil.isEquals(password, rpassword, "两次输入密码不一致");
        //判断手机号码是否注册
        if (this.checkPhone(phone)){
            throw new LogicException("手机号码已经注册");
        }
        //判断验证码是否一致
       String code= userInfoRedisService.getVerifyCode(phone);
        //equalsIgnoreCase字符串比较不区分大小写
        if (code==null || !code.equalsIgnoreCase(verifyCode)){
            throw new LogicException("验证码失效或错误");
        }
        UserInfo u = new UserInfo();
        u.setLevel(1);
        u.setHeadImgUrl("/images/default.jpg");
        u.setPhone(phone);
        u.setPassword(password);//需要加密
        u.setNickname(nickname);
        u.setState(UserInfo.STATE_NORMAL);
        userInfoMappe.insert(u);
    }

    @Override
    public String userLogin(String username, String password) throws LogicException{
        //查询用户
        UserInfo user = userInfoMappe.selectByUserNameAndPassword(username, password);
        //判断用户是否存在
        if(user==null){
            throw new LogicException("账号或密码错误");
        }
        //创建一个随机token
        String token = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        //返回一个token 把token作为key 用户作为对象存储在redis中
        userInfoRedisService.addToken(token,user);
        return token;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMappe.selectAll();
    }
}
